/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testdockerdb.carcontroller.services;

import com.google.gson.Gson;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import testdockerdb.carcontroller.DbSettings;
import testdockerdb.carcontroller.MqSettings;
import testdockerdb.carcontroller.MqSettings_Container;
import testdockerdb.carcontroller.models.CarPosInfo;

/**
 *
 * @author wuhao
 */
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix= "datasource")
public class MsgReceiver implements Runnable {

        private final static String QUEUE_NAME = "hello";
        ////final static String MQ_HOST="127.0.0.1";
        final static String MQ_HOST = "rabbitmq";
        final static Integer MQ_PORT = 5672;
        AmqpTemplate template = null;
        java.sql.Connection connection = null;
        DbOperation dbOpera = null;
  
        //MqSettings ms=new MqSettings();
        MqSettings_Container ms=new MqSettings_Container();
        
        public MsgReceiver() {
                MqInit();
                //dbOpera = new DbOperation();
        }

        void MqInit() {
                //String MQ_HOST=ms.getHost(),QUEUE_NAME=ms.getQueue();
                //Integer MQ_PORT=ms.getPort();
                
                try {
                        Thread.sleep(10000);
                } catch (InterruptedException ex) {
                        Logger.getLogger(MsgReceiver.class.getName()).log(Level.SEVERE, null, ex);
                }
                org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory
                        = new CachingConnectionFactory(MQ_HOST, MQ_PORT);

                AmqpAdmin admin = new RabbitAdmin(connectionFactory);
                admin.declareQueue(new Queue(QUEUE_NAME));
                template = new RabbitTemplate(connectionFactory);

        }

        void StartReceiving() {
                System.out.println("start listening rabbitmq...");

                //String QUEUE_NAME=ms.getQueue();
                while (true) {
                        String strData = (String) template.receiveAndConvert(QUEUE_NAME);
                        try {
                                if (!"".equals(strData) && strData != null) {
                                        System.out.println("MsgToDb received: " + strData);
                                        //解析
                                        //Gson gson = new Gson();
                                        //CarPosInfo carinfo = gson.fromJson(strData, CarPosInfo.class);

                                        //存数据库
                                        //dbOpera.SaveToDb(carinfo);
                                }

                                Thread.sleep(500);
                        } catch (InterruptedException ex) {
                                Logger.getLogger(MsgReceiver.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
        }

        @Override
        public void run() {
                StartReceiving();
        }
}
